package yxy.apple.protobuf.utility.generator;

public class TSConstantsGenerator extends ConstantsGenerator {
	private String namespace = "Noname";

	public TSConstantsGenerator(boolean trusted, String namespace) {
		super(trusted);
		this.namespace = namespace;
	}

	@Override
	protected String assembleHeader(int level, String type, String name, String comment) {
		String indentation = this.indent(level);
		StringBuffer buffer = new StringBuffer();
		if (comment != null) {
			buffer.append(String.format("\r\n%s//%s\r\n", indentation, comment));
		}
		if (level == 0 && type == null) {
			buffer.append("// ******************************\r\n");
			buffer.append("// 本文件由ProtobufCompiler自动生成\r\n");
			buffer.append("// *******************************\r\n");
			buffer.append(String.format("export namespace %s {\r\n", this.namespace));
		} else if (type != null && (type.equals("enum") || type.equals("class"))) {
			buffer.append(String.format("%sexport const enum %s {\r\n", indentation, name));
		} else {
			buffer.append(String.format("%s{\r\n", indentation));
		}
		return buffer.toString();
	}

	@Override
	protected String assembleFooter(int level, String type, String name, String comment) {
		String indentation = this.indent(level);
		return String.format("%s}\r\n", indentation);
	}

	@Override
	protected String assembleField(int level, String parent, String type, String name, String comment, String value) {
		String indentation = this.indent(level);
		StringBuffer buffer = new StringBuffer();
		if (comment != null) {
			buffer.append(String.format("%s//%s\r\n", indentation, comment));
		}
		if (parent != null && (parent.equals("enum") || parent.equals("class"))) {
			String format = "%s%s = %s,\r\n";
			if (type != null && type.equalsIgnoreCase("string")) {
				format = "%s%s = \"%s\",\r\n";
			}
			buffer.append(String.format(format, indentation, name, value));
		} else {
			String format = "%sexport const %s = %s;\r\n";
			if (type.endsWith("String")) {
				format = "%sexport const %s = \"%s\";\r\n";
			}
			buffer.append(String.format(format, indentation, name, value));
		}
		return buffer.toString();
	}

}
